package bw.hackathon.ub;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;



import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LaunchPage extends Activity {
	
	public static final String ID="Id";
	private Integer id;
	private EditText search;
	private Integer result;
	private Integer count;
	public String idd;
	private boolean databaseConnect;
	private ProgressDialog pDialog;
	JSONParser jsonParser = new JSONParser();
	private static String url_create_product = "http://www.tsbayford.com/android_connect/check_Id.php";
	private static final String TAG_SUCCESS = "success";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_launch_page);
		result=0;
		count=0;
		databaseConnect=false;
		search = (EditText) findViewById(R.id.search1);
		Button searchPatientButton = (Button) findViewById(R.id.startApp);
		searchPatientButton.setOnClickListener(new View.OnClickListener()
		{		
		@Override
		public void onClick(View v) {
			
			// TODO Auto-generated method stubs
			new CreateNewProduct().execute();

		}
	});
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.launch_page, menu);
		return true;
	}
	// Reaction to the menu selection
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.newPatientMenu:
			newPatient();
			return true;
		case R.id.updatePatientMenu:
			updatePatient();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}


	private void newPatient() {
		Intent i1 = new Intent(LaunchPage.this, Add_edit_patient.class);
		startActivity(i1);
	}
	private void updatePatient() {
		Intent i2 = new Intent(LaunchPage.this, search.class);
		startActivity(i2);
	}

	class CreateNewProduct extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(LaunchPage.this);
			pDialog.setMessage("Checking Id....");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Creating product
		 * */
		protected String doInBackground(String... args) {
			
			idd = search.getText().toString();

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", idd));

			// getting JSON Object
			// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(url_create_product,
					"POST", params);
			
			// check log cat for response
			Log.d("Create Response", json.toString());

			// check for success tag
			try {
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// successfully created product
					randomizer();
					
					// closing this screen
					
				} else {
					// failed to create product
					Intent i = new Intent(getApplicationContext(), TestReady.class);
					i.putExtra("message", "Invalid Id or Connection Failure Please Try Again");
					startActivity(i);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}

	}
	private void randomizer()
	{
		Random r = new Random();
		int var=0;
		var=r.nextInt(11)+1;
		switch (var){
		
		case 1:
			Intent i = new Intent(getApplicationContext(), TestPage.class);
			i.putExtra("count", count);
			i.putExtra("result", result);
			i.putExtra("Pid", idd);
			startActivity(i);
			break;
		case 2:
			Intent i1 = new Intent(getApplicationContext(), TestPage2.class);
			i1.putExtra("count", count);
			i1.putExtra("result", result);
			i1.putExtra("Pid", idd);
			startActivity(i1);
			break;
		case 3:
			Intent i2 = new Intent(getApplicationContext(), TestPage3.class);
			i2.putExtra("count", count);
			i2.putExtra("result", result);
			i2.putExtra("Pid", idd);
			startActivity(i2);
			break;
		case 4:
			Intent i3 = new Intent(getApplicationContext(), TestPage4.class);
			i3.putExtra("count", count);
			i3.putExtra("result", result);
			i3.putExtra("Pid", idd);
			startActivity(i3);
			break;
		case 5:
			Intent i4 = new Intent(getApplicationContext(), TestPage5.class);
			i4.putExtra("count", count);
			i4.putExtra("result", result);
			i4.putExtra("Pid", idd);
			startActivity(i4);
			break;
		case 6:
			Intent i5 = new Intent(getApplicationContext(), TestPage6.class);
			i5.putExtra("count", count);
			i5.putExtra("result", result);
			i5.putExtra("Pid", idd);
			startActivity(i5);
			break;
		case 7:
			Intent i6 = new Intent(getApplicationContext(), TestPage7.class);
			i6.putExtra("count", count);
			i6.putExtra("result", result);
			i6.putExtra("Pid", idd);
			startActivity(i6);
			break;
		case 8:
			Intent i7 = new Intent(getApplicationContext(), TestPage8.class);
			i7.putExtra("count", count);
			i7.putExtra("result", result);
			i7.putExtra("Pid", idd);
			startActivity(i7);
			break;
		case 9:
			Intent i8 = new Intent(getApplicationContext(), TestPage9.class);
			i8.putExtra("count", count);
			i8.putExtra("Pid", idd);
			i8.putExtra("result", result);
			startActivity(i8);
			break;
		case 10:
			Intent i9 = new Intent(getApplicationContext(), TestPage10.class);
			i9.putExtra("count", count);
			i9.putExtra("result", result);
			i9.putExtra("Pid", idd);
			startActivity(i9);
			break;
		case 11:
			Intent i10 = new Intent(getApplicationContext(), TestPage11.class);
			i10.putExtra("count", count);
			i10.putExtra("result", result);
			i10.putExtra("Pid", idd);
			startActivity(i10);
			break;
		
		}
		
	}

}
